<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\ChannelPeriod;
use App\Models\ChannelPeriod as ChannelPeriodModel;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class ChannelPeriodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid(): Grid
    {
        return Grid::make(new ChannelPeriod(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('channel_id');
            $grid->column('day_of_week');
            $grid->column('status');
            $grid->column('started_at');
            $grid->column('ended_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail(mixed $id): Show
    {
        return Show::make($id, new ChannelPeriod(), function (Show $show) {
            $show->field('id');
            $show->field('channel_id');
            $show->field('day_of_week');
            $show->field('status');
            $show->field('started_at');
            $show->field('ended_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form(): Form
    {
        return Form::make(new ChannelPeriod(), function (Form $form) {
            $form->disableDeleteButton();
            $form->disableViewButton();
            $form->disableListButton();

            // 获取频道ID
            $channelId = request()->get('channel_id');

            $form->display('id');
            $form->hidden('channel_id')->default($channelId);
            $form->select('day_of_week')->options(ChannelPeriodModel::getDayOfWeekList())->required();;
            $form->time('started_at');
            $form->time('ended_at');

            $form->switch('status');


            // 跳转并提示成功信息
            $form->saved(function (Form $form) {
                $channelId = $form->model()->channel_id;
                return $form->response()->success('保存成功')->redirect('/channels/' . $channelId);
            });
        });
    }
}
